<?php
class model_app_hits extends common_model {
	/**
	 * 基本表属性
	 */
	protected $table = 'app_hits';
	protected $pk = 'controller';
	private $controller = '';
	private $action = '';
	
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
		
		$this->controller = MODULE;
		$this->action = ACTION;
		
		// 点击
		$this->hit();
	}
	
	/**
	 * 是否存在此程序
	 */
	private function is_exists() {
		$res = $this->find("controller='{$this->controller}' AND action='{$this->action}'");
		return $res ? true : false;
	}
	
	/**
	 * 创建此程序
	 */
	private function app_create() {
		$data = array(
			'controller' => $this->controller,
			'action' => $this->action,
			'hits' => 1,
			'first' => time(),
			'last' => time(),
		);
		$this->create($data);
	}
	
	/**
	 * 更新程序点击数
	 */
	private function app_update() {
		$time = time();
		mysql_query("UPDATE ".DB_PREFIX."app_hits SET hits=hits+1, last=$time WHERE controller='{$this->controller}' AND action='{$this->action}'");
	}
	
	/**
	 * 点击程序执行
	 */
	private function hit() {
		$this->is_exists() ? $this->app_update() : $this->app_create();
	}
}